package com.baitengsoft.patentservicemonitor.api.controller;

import com.baitengsoft.patentservicemonitor.api.entity.*;
import com.baitengsoft.patentservicemonitor.api.model.ApplicantInfoModel;
import com.baitengsoft.patentservicemonitor.api.model.PacPatentInfoCountModel;
import com.baitengsoft.patentservicemonitor.api.service.*;
import io.swagger.annotations.*;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baitengsoft.patentservicemonitor.common.utils.Res;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;


/**
 * Created by 周俊 on 2017/12/14.
 */
@RestController
@RequestMapping("/api/v1/service")
@Api(value = "专利报表API", description = "用于插入、更新报表")
public class RptController {
    @Autowired
    private RptPatentDataAreaService rpPatentDataAreaService;

    @Autowired
    private SystemBaseService systemBaseService;

    @Autowired
    private RptPatentIpcService rptPatentIpcService;

    @Autowired
    private RptInventorService rptInventorService;

    @Autowired
    private RptAgencyService rptAgencyService;

    @Autowired
    private RptApplicantService rptApplicantService;

    @Autowired
    private PacApplicantService pacApplicantService;


    @ApiOperation(value = "更新万人相关数据")
    @GetMapping("/updateWanRen")
    public Res updateRptInfo(@RequestParam(name = "webPlatformId") @ApiParam(value = "平台id", required = true) Integer webPlatformId) {
        Integer webPlatformType = systemBaseService.queryPlatformTypeByPlatformId(webPlatformId);
        if (webPlatformType > 2) {
            systemBaseService.updatePacPatentInfo(webPlatformId);
            systemBaseService.updatePacPatentInfoOut(webPlatformId);
        } else {
            systemBaseService.updatePacPatentInfoCity(webPlatformId);
            systemBaseService.updatePacPatentInfoOutCity(webPlatformId);
        }

        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        List<Integer> webPlatformAreaIdList = systemBaseService.queryWebPlatformAreaList(webPlatformId);
//      for (int i = year - 1; i <= year; i++) {
        for (int i = 2018; i <= year; i++) {
            int maxMonth = 12;
            if (i == year) {
                maxMonth = month;
            }
            for (int j = 1; j <= maxMonth; j++) {
                //先写入未分类的数据
                addorUpdateRptPatentDataAreaInfo(0, webPlatformId, i, j);
                for (Integer id : webPlatformAreaIdList) {
                    addorUpdateRptPatentDataAreaInfo(id, webPlatformId, i, j);
                }
            }

        }


        return Res.ok();
    }


    /**
     * 写入专利报表,可以带参数
     */
    @ApiOperation(value = "插入或更新报表")
    @RequestMapping(value = "addOrUpdateRpt", method = RequestMethod.GET)
    @ApiImplicitParams(value =
            {@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "webPlatformId", value = "平台id", required = false)
                    , @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "webPlatformAreaId", value = "区域id", required = false)})
    public Res addOrUpdateRpt(Integer webPlatformId, Integer webPlatformAreaId) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("开始更新" + sdf.format(new Date()));
            if (webPlatformAreaId != null) {
                webPlatformId = systemBaseService.queryPlatformIdByAreaId(webPlatformAreaId);
                //更新落地专利信息的区域ID
                Integer webPlatformType = systemBaseService.queryPlatformTypeByPlatformId(webPlatformId);
                if (webPlatformType > 2) {
                    systemBaseService.updatePacPatentInfo(webPlatformId);
                    systemBaseService.updatePacPatentInfoOut(webPlatformId);
                } else {
                    systemBaseService.updatePacPatentInfoCity(webPlatformId);
                    systemBaseService.updatePacPatentInfoOutCity(webPlatformId);
                }
                addorUpdateRptPatentDataAreaInfo(Integer.valueOf("0"), webPlatformAreaId);//先写入未分类的数据
                addorUpdateRptPatentDataAreaInfo(webPlatformAreaId, webPlatformAreaId);
                addorUpdateRptPatentIpcInfo(webPlatformId);
                addorUpdateRptInventorInfo(webPlatformId);
                addorUpdateRptAgencyInfo(webPlatformId);
                addorUpdateRptApplicantInfo(webPlatformId);
            } else {
                if (webPlatformId != null) {
                    //更新落地专利信息的区域ID
                    Integer webPlatformType = systemBaseService.queryPlatformTypeByPlatformId(webPlatformId);
                    if (webPlatformType > 2) {
                        systemBaseService.updatePacPatentInfo(webPlatformId);
                        systemBaseService.updatePacPatentInfoOut(webPlatformId);
                    } else {
                        systemBaseService.updatePacPatentInfoCity(webPlatformId);
                        systemBaseService.updatePacPatentInfoOutCity(webPlatformId);
                    }
                    List<Integer> webPlatformAreaIdList = systemBaseService.queryWebPlatformAreaList(webPlatformId);
                    addorUpdateRptPatentDataAreaInfo(0, webPlatformId);//先写入未分类的数据
                    for (Integer id : webPlatformAreaIdList) {
                        addorUpdateRptPatentDataAreaInfo(id, webPlatformId);
                    }
                    addorUpdateRptPatentIpcInfo(webPlatformId);
                    addorUpdateRptInventorInfo(webPlatformId);
                    addorUpdateRptAgencyInfo(webPlatformId);
                    addorUpdateRptApplicantInfo(webPlatformId);
                } else {
                    if (webPlatformId == null && webPlatformAreaId == null) {
                        List<Integer> webPlatformIdList = systemBaseService.queryWebPlatformList();
                        for (Integer id : webPlatformIdList) {
                            //更新落地专利信息的区域ID
                            Integer webPlatformType = systemBaseService.queryPlatformTypeByPlatformId(id);
                            if (webPlatformType > 2) {
                                systemBaseService.updatePacPatentInfo(id);
                                systemBaseService.updatePacPatentInfoOut(id);
                            } else {
                                systemBaseService.updatePacPatentInfoCity(id);
                                systemBaseService.updatePacPatentInfoOutCity(id);
                            }
                            addorUpdateRptPatentDataAreaInfo(0, id);//先写入未分类的数据
                            List<Integer> webPlatformAreaIdList = systemBaseService.queryWebPlatformAreaList(id);
                            for (Integer areaId : webPlatformAreaIdList) {
                                addorUpdateRptPatentDataAreaInfo(areaId, id);
                            }
                            addorUpdateRptPatentIpcInfo(id);
                            addorUpdateRptInventorInfo(id);
                            addorUpdateRptAgencyInfo(id);
                            addorUpdateRptApplicantInfo(id);
                        }
                    }
                }
            }
            System.out.println(sdf.format(new Date()));
            return Res.ok().put("msg", "保存成功");
        } catch (Exception e) {
            return Res.error().put("msg", "错误信息：" + e);
        }
    }

    /**
     * 根据区域ID和所属平台ID统计时间区域专利报表(万人相关报表)
     *
     * @param webPlatformAreaId
     * @param webPlatformID
     * @param year
     * @param month
     */
    public void addorUpdateRptPatentDataAreaInfo(Integer webPlatformAreaId, Integer webPlatformID, Integer year, Integer month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Integer dateYM = Integer.valueOf(year.toString() + String.format("%02d", month));
        //获取RptPatentDataArea报表对象，有则update，没有则add
        RptPatentDataAreaEntity rptPatentDataAreaEntity = rpPatentDataAreaService.queryRptPatentDataAreaByQuery
                (webPlatformAreaId, dateYM, webPlatformID);
        if (rptPatentDataAreaEntity != null) {
            rptPatentDataAreaEntity.setUpdateDate(new Date());//更新时间
        } else {
            rptPatentDataAreaEntity = new RptPatentDataAreaEntity();
            rptPatentDataAreaEntity.setID(0);
            rptPatentDataAreaEntity.setCreateDate(new Date());//创建时间
        }
        rptPatentDataAreaEntity.setDateYM(dateYM);//时间维度：YYYYYMM
        rptPatentDataAreaEntity.setPlatformID(webPlatformID);//所属平台ID
        rptPatentDataAreaEntity.setAreaID(webPlatformAreaId);//区域
        rptPatentDataAreaEntity.setIsDel(false);


        List<CfgPatentIndicatorEntity> cfgPatentIndicatorEntityList = systemBaseService.queryCfgPatentIndicatorList
                (year, webPlatformAreaId);
        CfgPatentIndicatorEntity cfgPatentIndicatorEntity = new CfgPatentIndicatorEntity();//专利指标
        if (cfgPatentIndicatorEntityList.size() > 0) {
            cfgPatentIndicatorEntity = cfgPatentIndicatorEntityList.get(0);
            rptPatentDataAreaEntity.setPopulation(cfgPatentIndicatorEntity.getPopulation());//人口数量（万人)
            rptPatentDataAreaEntity.setYXFMTargetNum(cfgPatentIndicatorEntity.getYXFMCount());//有效发明-指标/目标
            rptPatentDataAreaEntity.setYXFMWRTargetNum(cfgPatentIndicatorEntity.getYXFMWRCount());//万人有效发明-指标/目标
            rptPatentDataAreaEntity.setYXZLTargetNum(cfgPatentIndicatorEntity.getYXZLCount());//有效专利-指标/目标
            rptPatentDataAreaEntity.setYXZLWRTargetNum(cfgPatentIndicatorEntity.getYXZLWRCount());//万人-有效专利-指标/目标
            rptPatentDataAreaEntity.setZLSQTargetNum(cfgPatentIndicatorEntity.getZLSQCount());//专利申请-指标/目标
            rptPatentDataAreaEntity.setFMSQTargetNum(cfgPatentIndicatorEntity.getFMSQCount());//发明专利申请-指标/目标
            rptPatentDataAreaEntity.setSQZLTargetNum(cfgPatentIndicatorEntity.getSQZLCount());//专利授权-指标/目标
            rptPatentDataAreaEntity.setSQZLWRTargetNum(cfgPatentIndicatorEntity.getSQZLWRCount());//万人-专利授权-指标/目标
            rptPatentDataAreaEntity.setSQZLFMTargetNum(cfgPatentIndicatorEntity.getSQZLFMCount());//发明专利授权-指标/目标
            rptPatentDataAreaEntity.setSQZLFMWRTargetNum(cfgPatentIndicatorEntity.getSQZLFMWRCount());//万人-发明专利授权-指标/目标
            rptPatentDataAreaEntity.setSZSCTargetNum(cfgPatentIndicatorEntity.getSZSCCount());//实质审查-指标/目标
        } else {
            rptPatentDataAreaEntity.setPopulation(BigDecimal.ZERO);//人口数量（万人)
            rptPatentDataAreaEntity.setYXFMTargetNum(Integer.valueOf("0"));//有效发明-指标/目标
            rptPatentDataAreaEntity.setYXFMWRTargetNum(BigDecimal.ZERO);//万人有效发明-指标/目标
            rptPatentDataAreaEntity.setYXZLTargetNum(Integer.valueOf("0"));//有效专利-指标/目标
            rptPatentDataAreaEntity.setYXZLWRTargetNum(BigDecimal.ZERO);//万人-有效专利-指标/目标
            rptPatentDataAreaEntity.setZLSQTargetNum(Integer.valueOf("0"));//专利申请-指标/目标
            rptPatentDataAreaEntity.setFMSQTargetNum(Integer.valueOf("0"));//发明专利申请-指标/目标
            rptPatentDataAreaEntity.setSQZLTargetNum(Integer.valueOf("0"));//专利授权-指标/目标
            rptPatentDataAreaEntity.setSQZLWRTargetNum(BigDecimal.ZERO);//万人-专利授权-指标/目标
            rptPatentDataAreaEntity.setSQZLFMTargetNum(Integer.valueOf("0"));//发明专利授权-指标/目标
            rptPatentDataAreaEntity.setSQZLFMWRTargetNum(BigDecimal.ZERO);//万人-发明专利授权-指标/目标
            rptPatentDataAreaEntity.setSZSCTargetNum(Integer.valueOf("0"));//实质审查-指标/目标
        }

//        获取专利数据汇总
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(year, month - 1, 1);
        calendar.add(Calendar.MONTH, 1);
        Date nextDate = calendar.getTime();
        String startDate = dateYM + "01";
        String endDate = sdf.format(nextDate) + "01";
        List<PacPatentInfoCountModel> pacPatentInfoCountModelList = systemBaseService.queryPacPatentinfoCount
                (webPlatformAreaId, webPlatformID, startDate, endDate);

        //计算各个指标值------开始

        //有效专利-发明-当月数量
        Integer YXFMCount = pacPatentInfoCountModelList.get(0).getCou();
        //有效专利-实用-当月数量
        Integer YXSYCount = pacPatentInfoCountModelList.get(1).getCou();
        //有效专利-外观-当月数量
        Integer YXWGCount = pacPatentInfoCountModelList.get(2).getCou();
        //申请专利-发明-当月数量
        Integer ZLSQFMCount = pacPatentInfoCountModelList.get(3).getCou();
        //申请专利-实用-当月数量
        Integer ZLSQSYCount = pacPatentInfoCountModelList.get(4).getCou();
        //申请专利-外观-当月数量
        Integer ZLSQWGCount = pacPatentInfoCountModelList.get(5).getCou();
        //授权专利-发明-当月数量
        Integer SQZLFMCount = pacPatentInfoCountModelList.get(6).getCou();
        //授权专利-实用-当月数量
        Integer SQZLSYCount = pacPatentInfoCountModelList.get(7).getCou();
        //授权专利-外观-当月数量
        Integer SQZLWGCount = pacPatentInfoCountModelList.get(8).getCou();
        //实质审查-当前为止累计数量
        Integer SZSCTotalNum = pacPatentInfoCountModelList.get(9).getCou();

        //有效专利-发明-当月数量
        rptPatentDataAreaEntity.setYXFMCount(YXFMCount);
        //有效专利-实用-当月数量
        rptPatentDataAreaEntity.setYXSYCount(YXSYCount);
        //有效专利-外观-当月数量
        rptPatentDataAreaEntity.setYXWGCount(YXWGCount);
        //有效专利-合计-当月数量
        rptPatentDataAreaEntity.setYXZLCount(YXFMCount + YXSYCount + YXWGCount);
        //申请专利-发明-当月数量
        rptPatentDataAreaEntity.setZLSQFMCount(ZLSQFMCount);
        //申请专利-实用-当月数量
        rptPatentDataAreaEntity.setZLSQSYCount(ZLSQSYCount);
        //申请专利-外观-当月数量
        rptPatentDataAreaEntity.setZLSQWGCount(ZLSQWGCount);
        //申请专利-合计-当月数量
        rptPatentDataAreaEntity.setZLSQHJCount(ZLSQFMCount + ZLSQSYCount + ZLSQWGCount);
        //授权专利-发明-当月数量
        rptPatentDataAreaEntity.setSQZLFMCount(SQZLFMCount);
        //授权专利-实用-当月数量
        rptPatentDataAreaEntity.setSQZLSYCount(SQZLSYCount);
        //授权专利-外观-当月数量
        rptPatentDataAreaEntity.setSQZLWGCount(SQZLWGCount);
        //授权专利-合计-当月数量
        rptPatentDataAreaEntity.setSQZLHJCount(SQZLFMCount + SQZLSYCount + SQZLWGCount);
        //实质审查-当前为止累计数量
        rptPatentDataAreaEntity.setSZSCTotalNum(SZSCTotalNum);
        //实质审查-完成率
        rptPatentDataAreaEntity.setSZSCCompletionRate(getNum(BigDecimal.valueOf(SZSCTotalNum), BigDecimal.valueOf(rptPatentDataAreaEntity.getSZSCTargetNum())));
        //有效发明-当月为止累计数量
        rptPatentDataAreaEntity.setYXFMTotalNum(YXFMCount);
        //有效发明-完成率
        rptPatentDataAreaEntity.setYXFMCompletionRate(getNum(BigDecimal.valueOf(rptPatentDataAreaEntity.getYXFMTotalNum()), BigDecimal.valueOf(rptPatentDataAreaEntity.getYXFMTargetNum())));
        //万人有效发明专利拥有数量
        rptPatentDataAreaEntity.setYXFMWRTotalNum(getNum(BigDecimal.valueOf(rptPatentDataAreaEntity.getYXFMCount()), rptPatentDataAreaEntity.getPopulation()));
        //万人有效发明完成率
        rptPatentDataAreaEntity.setYXFMWRCompletionRate(getNum(rptPatentDataAreaEntity.getYXFMWRTotalNum(), rptPatentDataAreaEntity.getYXFMWRTargetNum()));
        //有效专利-当月为止累计数量
        rptPatentDataAreaEntity.setYXZLTotalNum(YXFMCount + YXSYCount + YXWGCount);
        //有效专利-完成率
        rptPatentDataAreaEntity.setYXZLCompletionRate(getNum(BigDecimal.valueOf(rptPatentDataAreaEntity.getYXZLTotalNum()), BigDecimal.valueOf(rptPatentDataAreaEntity.getYXZLTargetNum())));
        //万人-有效专利-当月为止累计数量
        rptPatentDataAreaEntity.setYXZLWRTotalNum(getNum(BigDecimal.valueOf(rptPatentDataAreaEntity.getYXZLTotalNum()), rptPatentDataAreaEntity.getPopulation()));
        //万人-有效专利-完成率
        rptPatentDataAreaEntity.setYXZLWRCompletionRate(getNum(rptPatentDataAreaEntity.getYXZLWRTotalNum(), rptPatentDataAreaEntity.getYXZLWRTargetNum()));

        //获取前一个月的报表数据，用于计算当前月的累计数据，如果是1月份，则不需要累计-------开始
        Integer ZLSQTotalNum = rptPatentDataAreaEntity.getZLSQHJCount();
        Integer FMSQTotalNum = rptPatentDataAreaEntity.getZLSQFMCount();
        Integer SQZLTotalNum = rptPatentDataAreaEntity.getSQZLHJCount();
        BigDecimal SQZLWRTotalNum = getNum(BigDecimal.valueOf(rptPatentDataAreaEntity.getSQZLHJCount()), rptPatentDataAreaEntity.getPopulation());
        Integer SQZLFMTotalNum = rptPatentDataAreaEntity.getSQZLFMCount();
        BigDecimal SQZLFMWRTotalNum = getNum(BigDecimal.valueOf(rptPatentDataAreaEntity.getSQZLFMCount()), rptPatentDataAreaEntity.getPopulation());
        if (month > 1) {

            Calendar calendarLast = Calendar.getInstance();
            calendarLast.clear();
            calendarLast.set(year, month - 2, 1);
            Date lastDate = calendarLast.getTime();
            Integer lastDateYM = Integer.valueOf(sdf.format(lastDate));
            //获取RptPatentDataArea报表对象，有则update，没有则add
            RptPatentDataAreaEntity rptPatentDataAreaEntityList = rpPatentDataAreaService.queryRptPatentDataAreaByQuery
                    (webPlatformAreaId, lastDateYM, webPlatformID);
            if (rptPatentDataAreaEntityList != null) {
                ZLSQTotalNum = rptPatentDataAreaEntityList.getZLSQTotalNum() + ZLSQTotalNum;
                FMSQTotalNum = rptPatentDataAreaEntityList.getFMSQTotalNum() + FMSQTotalNum;
                SQZLTotalNum = rptPatentDataAreaEntityList.getSQZLTotalNum() + SQZLTotalNum;
                SQZLWRTotalNum = rptPatentDataAreaEntityList.getSQZLWRTotalNum().add(SQZLWRTotalNum);
                SQZLFMTotalNum = rptPatentDataAreaEntityList.getSQZLFMTotalNum() + SQZLFMTotalNum;
                //万人-发明专利授权-当前为止累计数量
                SQZLFMWRTotalNum = rptPatentDataAreaEntityList.getSQZLFMWRTotalNum().add(SQZLFMWRTotalNum);
            }
        }

        rptPatentDataAreaEntity.setZLSQTotalNum(ZLSQTotalNum);//专利申请-当前为止累计数量
        rptPatentDataAreaEntity.setZLSQCompletionRate(getNum(BigDecimal.valueOf(ZLSQTotalNum), BigDecimal.valueOf(rptPatentDataAreaEntity.getZLSQTargetNum())));//专利申请-完成率
        rptPatentDataAreaEntity.setFMSQTotalNum(FMSQTotalNum);//发明专利申请-当前为止累计数量
        rptPatentDataAreaEntity.setFMSQCompletionRate(getNum(BigDecimal.valueOf(FMSQTotalNum), BigDecimal.valueOf(rptPatentDataAreaEntity.getFMSQTargetNum())));//发明专利申请-完成率
        rptPatentDataAreaEntity.setSQZLTotalNum(SQZLTotalNum);//专利授权-当前为止累计数量
        rptPatentDataAreaEntity.setSQZLCompletionRate(getNum(BigDecimal.valueOf(SQZLTotalNum), BigDecimal.valueOf(rptPatentDataAreaEntity.getSQZLTargetNum())));//专利授权-完成率
        rptPatentDataAreaEntity.setSQZLWRTotalNum(SQZLWRTotalNum);//万人-专利授权-当前为止累计数量
        rptPatentDataAreaEntity.setSQZLWRCompletionRate(getNum(SQZLWRTotalNum, rptPatentDataAreaEntity.getSQZLWRTargetNum()));//万人-专利授权-完成率
        rptPatentDataAreaEntity.setSQZLFMTotalNum(SQZLFMTotalNum);//发明专利授权-当前为止累计数量
        rptPatentDataAreaEntity.setSQZLFMCompletionRate(getNum(BigDecimal.valueOf(SQZLFMTotalNum), BigDecimal.valueOf(rptPatentDataAreaEntity.getSQZLFMTargetNum())));//发明专利授权-完成率
        rptPatentDataAreaEntity.setSQZLFMWRTotalNum(SQZLFMWRTotalNum);//万人-发明专利授权-当前为止累计数量
        rptPatentDataAreaEntity.setSQZLFMWRCompletionRate(getNum(SQZLFMWRTotalNum, rptPatentDataAreaEntity.getSQZLFMWRTargetNum()));//万人-发明专利授权-完成率
        //获取前一个月的报表数据，用于计算当前月的累计数据，如果是1月份，则不需要累计-------结束
        //计算各个指标值------结束

        rpPatentDataAreaService.addorUpdateRptPatentDataAreaInfo(rptPatentDataAreaEntity);
    }


    /**
     * 根据区域ID和所属平台ID统计时间区域专利报表(万人相关报表)
     *
     * @param webPlatformAreaId 区域ID
     * @param webPlatformID     所属平台ID
     */
    public void addorUpdateRptPatentDataAreaInfo(Integer webPlatformAreaId, Integer webPlatformID) {

        Calendar calendar = Calendar.getInstance();
        addorUpdateRptPatentDataAreaInfo(webPlatformAreaId, webPlatformID, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1);


    }

    /**
     * 根据所属平台ID统计专利IPC分类报表
     *
     * @param webPlatformID 所属平台ID
     */
    public void addorUpdateRptPatentIpcInfo(Integer webPlatformID) {
        List<CfgIpcEntity> cfgIpcEntityList = systemBaseService.queryCfgIpcList(Integer.parseInt("0"));
        for (CfgIpcEntity cfgIpcEntity : cfgIpcEntityList) {
            List<PacPatentInfoCountModel> pacPatentInfoCountModelList = systemBaseService.queryPacPatentinfoForIpcCount(webPlatformID, cfgIpcEntity.getCode().substring(0, 1));
            Integer YXFMCount = pacPatentInfoCountModelList.get(0).getCou();
            Integer YXSYCount = pacPatentInfoCountModelList.get(1).getCou();
            Integer YXWGCount = pacPatentInfoCountModelList.get(2).getCou();
            RptPatentIpcEntity rptPatentIpcEntity = rptPatentIpcService.queryRptPatentIpcByQuery(webPlatformID,
                    cfgIpcEntity.getID());
            if (rptPatentIpcEntity == null) {
                rptPatentIpcEntity = new RptPatentIpcEntity();
                rptPatentIpcEntity.setID(Integer.parseInt("0"));
                rptPatentIpcEntity.setPlatformID(webPlatformID);
                rptPatentIpcEntity.setParentID(cfgIpcEntity.getID());
            }

            rptPatentIpcEntity.setCode(cfgIpcEntity.getCode());
            rptPatentIpcEntity.setName(cfgIpcEntity.getName());
            rptPatentIpcEntity.setYXFMCount(YXFMCount);
            rptPatentIpcEntity.setYXSYCount(YXSYCount);
            rptPatentIpcEntity.setYXWGCount(YXWGCount);
            rptPatentIpcEntity.setYXHZCount(YXFMCount + YXSYCount + YXWGCount);
            rptPatentIpcService.addorUpdateRptPatentIpcInfo(rptPatentIpcEntity);
        }
    }

    /**
     * 根据所属平台ID统计发明人报表
     *
     * @param webPlatformID 所属平台ID
     */
    public void addorUpdateRptInventorInfo(Integer webPlatformID) {
        rptInventorService.deleteRptInventor(webPlatformID);//删除此平台报表数据
        List<PacPatentInfoCountModel> pacPatentInfoCountModelList = systemBaseService.queryPacPatentinfoForInventorCount(webPlatformID);
        int i = 0;
        for (PacPatentInfoCountModel model : pacPatentInfoCountModelList) {
            if (i < 15) {//只统计合计最大的15条数据
                Integer YXFMCount = model.getFmCount();
                Integer YXSYCount = model.getSyCount();
                Integer YXWGCount = model.getWgCount();

                RptInventorEntity rptInventorEntity = new RptInventorEntity();
                rptInventorEntity.setID(Integer.parseInt("0"));
                rptInventorEntity.setPlatformID(webPlatformID);
                rptInventorEntity.setInventorName(model.getInventPerson());
                rptInventorEntity.setYXFMCount(YXFMCount);
                rptInventorEntity.setYXSYCount(YXSYCount);
                rptInventorEntity.setYXWGCount(YXWGCount);
                rptInventorEntity.setYXHZCount(YXFMCount + YXSYCount + YXWGCount);
                rptInventorService.addorUpdateRptInventorInfo(rptInventorEntity);
                i++;
            } else {
                break;
            }
        }
    }

    /**
     * 根据所属平台ID统计代理机构报表
     *
     * @param webPlatformID 所属平台ID
     */
    public void addorUpdateRptAgencyInfo(Integer webPlatformID) {
        rptAgencyService.deleteRptAgency(webPlatformID);

        //统计所有专利总量
        List<PacPatentInfoCountModel> pacPatentInfoCountModelList = systemBaseService.queryPacPatentinfoForAgencyCount(webPlatformID, Integer.valueOf("2"));
        int i = 0;
        for (PacPatentInfoCountModel model : pacPatentInfoCountModelList) {
            if (i < 15) {//只统计合计最大的15条数据
                Integer YXFMCount = model.getFmCount();
                Integer YXSYCount = model.getSyCount();
                Integer YXWGCount = model.getWgCount();
                Integer ApplicantCount = systemBaseService.queryPacPatentinfoForAgencyApplyPersonCount(model.getAgencyName(), webPlatformID, Integer.valueOf("2"));

                RptAgencyEntity rptAgencyEntity = new RptAgencyEntity();
                rptAgencyEntity.setID(Integer.parseInt("0"));
                rptAgencyEntity.setPlatformID(webPlatformID);
                rptAgencyEntity.setDataType(Integer.valueOf("2"));
                rptAgencyEntity.setAgencyName(model.getAgencyName());
                rptAgencyEntity.setApplicantCount(ApplicantCount);
                rptAgencyEntity.setYXFMCount(YXFMCount);
                rptAgencyEntity.setYXSYCount(YXSYCount);
                rptAgencyEntity.setYXWGCount(YXWGCount);
                rptAgencyEntity.setYXHZCount(YXFMCount + YXSYCount + YXWGCount);
                rptAgencyService.addorUpdateRptAgencyInfo(rptAgencyEntity);
                i++;
            } else {
                break;
            }
        }

        //统计有效专利总量
        List<PacPatentInfoCountModel> pacPatentInfoCountModelList1 = systemBaseService.queryPacPatentinfoForAgencyCount(webPlatformID, Integer.valueOf("1"));
        int j = 0;
        for (PacPatentInfoCountModel model : pacPatentInfoCountModelList1) {
            if (j < 15) {//只统计合计最大的15条数据
                Integer YXFMCount = model.getFmCount();
                Integer YXSYCount = model.getSyCount();
                Integer YXWGCount = model.getWgCount();
                Integer ApplicantCount = systemBaseService.queryPacPatentinfoForAgencyApplyPersonCount(model.getAgencyName(), webPlatformID, Integer.valueOf("2"));

                RptAgencyEntity rptAgencyEntity = new RptAgencyEntity();
                rptAgencyEntity.setID(Integer.parseInt("0"));
                rptAgencyEntity.setPlatformID(webPlatformID);
                rptAgencyEntity.setDataType(Integer.valueOf("1"));
                rptAgencyEntity.setAgencyName(model.getAgencyName());
                rptAgencyEntity.setApplicantCount(ApplicantCount);
                rptAgencyEntity.setYXFMCount(YXFMCount);
                rptAgencyEntity.setYXSYCount(YXSYCount);
                rptAgencyEntity.setYXWGCount(YXWGCount);
                rptAgencyEntity.setYXHZCount(YXFMCount + YXSYCount + YXWGCount);
                rptAgencyService.addorUpdateRptAgencyInfo(rptAgencyEntity);
                j++;
            } else {
                break;
            }
        }
    }

    /**
     * 根据所属平台ID统计权利人报表,更新权利人表、专利信息转入转出表中的权利人ID
     *
     * @param webPlatformID 所属平台ID
     */
    public void addorUpdateRptApplicantInfo(Integer webPlatformID) {
        systemBaseService.deleteTempRptApplicant(webPlatformID);//删除当前平台临时表数据
        //将权利人数据插入临时表中
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        String startDate = String.valueOf(year) + "-01-01 00:00:00";
        String endDate = String.valueOf(year + 1) + "-01-01 00:00:00";
        systemBaseService.saveTempRptApplicant(webPlatformID, startDate, endDate);

        //将当前平台的权利人状态设置为0，更新或插入所有的权利人以后，将平台中所有isupdate=0的数据删除
        pacApplicantService.updatePacApplicant(webPlatformID, Integer.valueOf("0"), null);

        //删除当前平台所有权利人报表数据
        rptApplicantService.deleteRptApplicant(webPlatformID, null);

        //获取本次更新所有权利人
        List<ApplicantInfoModel> applicantInfoModellist = systemBaseService.queryApplicantInfoList(webPlatformID);

        int threadNum = applicantInfoModellist.size() < 300 ? applicantInfoModellist.size() : 300;
        ThreadPoolExecutor executor = new ThreadPoolExecutor(200, threadNum, 300,
                TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100),
                new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < threadNum; i++) {
            executor.execute(new RptThread(i, threadNum, webPlatformID, applicantInfoModellist, systemBaseService,
                    pacApplicantService, rptApplicantService));
        }
        executor.shutdown();
        while (true) {
            if (executor.isTerminated()) {
                break;
            }
        }
        pacApplicantService.deletePacApplicant(webPlatformID);//删除当前平台所有isupdate=0的数据，isupdate=0的条件已在sql中默认写入

        systemBaseService.updatePacPatentInfoOutApplyId(webPlatformID);//更新转出专利信息表中的权利人ID

        systemBaseService.updatePacPatentInfoApplyId(webPlatformID);//更新转入专利信息表中的权利人ID

        systemBaseService.deleteTempRptApplicant(webPlatformID);//删除当前平台临时表数据
    }

    public BigDecimal getNum(BigDecimal num1, BigDecimal num2) {
        BigDecimal returnNum;
        if (num2 == null || num2.compareTo(BigDecimal.ZERO) == 0) {
            returnNum = BigDecimal.ZERO;
        } else {
            returnNum = num1.multiply(new BigDecimal("100")).divide(num2, 2, BigDecimal.ROUND_HALF_EVEN);
        }
        return returnNum;
    }
}

class RptThread implements Runnable {

    private int num;

    private int threadNum;

    private int webPlatformID;

    private List<ApplicantInfoModel> applicantInfoModelList;

    private SystemBaseService systemBaseService;

    private PacApplicantService pacApplicantService;

    private RptApplicantService rptApplicantService;

    public RptThread(int num, int threadNum, int webPlatformID, List<ApplicantInfoModel> applicantInfoModelList, SystemBaseService systemBaseService, PacApplicantService pacApplicantService, RptApplicantService rptApplicantService) {
        this.num = num;
        this.threadNum = threadNum;
        this.systemBaseService = systemBaseService;
        this.pacApplicantService = pacApplicantService;
        this.rptApplicantService = rptApplicantService;
        this.webPlatformID = webPlatformID;
        this.applicantInfoModelList = applicantInfoModelList;
    }

    public void run() {
        int length = 0;
        for (ApplicantInfoModel applicantInfoModel : applicantInfoModelList) {
            if (length % threadNum == num) {
                try {
                    PacApplicantEntity pacApplicantEntity = pacApplicantService.queryPacApplicant(webPlatformID, applicantInfoModel.getApplyPerson(), applicantInfoModel.getApplyPersonType(), applicantInfoModel.getPatAddressLast());

                    if (pacApplicantEntity == null) {//不存在当前权利人，先新增，再获取
                        pacApplicantEntity = new PacApplicantEntity();
                        pacApplicantEntity.setID(Integer.valueOf("0"));
                        pacApplicantEntity.setPlatformID(webPlatformID);
                        pacApplicantEntity.setPatentID(Integer.valueOf("0"));
                        pacApplicantEntity.setName(applicantInfoModel.getApplyPerson());
                        pacApplicantEntity.setTypeLevel1(applicantInfoModel.getApplyPersonType());
                        pacApplicantEntity.setTypeLevel2("");
                        pacApplicantEntity.setAddress(applicantInfoModel.getPatAddressLast());
                        pacApplicantEntity.setAreaID(applicantInfoModel.getAreaID());
                        pacApplicantEntity.setIndustryID(Integer.valueOf("0"));
                        pacApplicantEntity.setIsUpdate(Integer.valueOf("1"));
                        pacApplicantEntity.setCreateDate(new Date());
                        pacApplicantEntity.setUpdateDate(new Date());
                        pacApplicantEntity.setCreateOperID(Integer.valueOf("0"));
                        pacApplicantEntity.setUpdateOperID(Integer.valueOf("0"));
                        pacApplicantService.addPacApplicant(pacApplicantEntity);

//                        //重新获取当前权利人，主要是要获取权利人ID
//                        pacApplicantEntity = pacApplicantService.queryPacApplicant(webPlatformID,applicantInfoModel.getApplyPerson(), applicantInfoModel.getApplyPersonType(), applicantInfoModel.getPatAddressLast());
                    } else {
                        pacApplicantService.updatePacApplicant(webPlatformID, Integer.valueOf("1"), pacApplicantEntity.getID());//如果记录存在则更新isupdate=1
                    }

                    if (pacApplicantEntity != null) {
                        rptApplicantService.insertRptApplicant(webPlatformID, pacApplicantEntity.getID(), pacApplicantEntity.getAddress(), pacApplicantEntity.getName(), pacApplicantEntity.getTypeLevel1());
                    }

                    System.out.println("线程" + num + ":权利人名称：" + pacApplicantEntity.getName() + "权利人ID：" + pacApplicantEntity.getID());
                } catch (Exception e) {
                    System.out.println("错误信息:" + e);
                }
            }
            length++;
        }
    }
}
